const text = {
  MakeSolution:{
    en: "Make Solutions",
    zh: "制作溶液",
  },
  Concentration:{
    en: "Concentration",
    zh: "浓度",
  },
  Volume:{
    en: "Volume",
    zh: "容量",
  },
  MolecularWeight:{
    en: "Molecular Weight",
    zh: "分子量",
  },
  Calculate:{
    en: "Calculate",
    zh: "计算",
  },
  Reset:{
    en: "Reset",
    zh: "重置",
  },
  MakeSolutionResHead:{
    en:"You will need",
    zh:"你需要",
  },
  MakeSolutionResTail:{
    en:"compound to make the solution.",
    zh:"化合物来制作该溶液。",
  },


  DiluteStock:{
    en:"Dilute Stock",
    zh:"稀释原液",
  },
  Stock:{
    en:"Stock",
    zh:"原液浓度",
  },
  Final:{
    en:"Final",
    zh:"最终浓度",
  },
  DiluteStockResTail:{
    en:"stock for the dilution",
    zh:"原液来稀释",
  },
}

const observeText = [
  ['朋', '明'],
  ['乌', '鸟'], 
  ['人', '入'],
  ['干', '于'],
  ['目', '田'],
  ['甲', '申'],
  ['王', '玉'],
  ['慕', '幕'],
  ['诞', '涎'],
  ['掩', '俺'],
  ['详', '洋'],
  ['谣', '瑶'],
  ['弛', '驰'],
  ['译', '泽'],
  ['咏', '脉'],
  ['拥', '佣'],
  ['坳', '拗'],
  ['挑', '桃'],
]

function makeLanguageText(lang){
  var useText = {}
  for (let id in text){
    let value = text[id]
    useText[id] = value[lang]
  }
  return useText
}

const URL = "https://www.iteasywords.cn"

async function HttpPost(type,data){
  var app = getApp()
  if (app && app.globalData && app.globalData.openid){
    data.openid = app.globalData.openid
  }
  data.type = type
  // 获取到登录凭证 code
  return new Promise(function(reslove,reject){
      console.log("HttpPost start type", type, "data", data)
     wx.request({
        url: URL + "/api", // 替换成您的服务器接口地址
        method: 'POST', // 设置请求方法为 POST
        data: data,
        success: (response) => {
           console.log('wx.request成功', response, type, data)
           if (response.data.code == 0){
              reslove(response.data);
           }else{
              reject(response);
           }
        },
        fail: (err) => {
           console.error('wx.request失败', err)
        }
     })
  })
}

function timestampToTime(timestamp) {
  timestamp = timestamp ? timestamp : null;
  let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}

function timestampToDate(timestamp) {
  timestamp = timestamp ? timestamp : null;
  let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  return Y + M + D
}



var exerciseTypes = {
  yujia:{
    costSpeed:0.03,
    name:"瑜伽",
    gif : "https://www.iteasywords.cn/static/yujia0-min.gif",
    music:"",
  },
  walk:{
    costSpeed:0.04,
    name:"行走",
    gif : "https://www.iteasywords.cn/static/walk1.gif",
    speedUp:true,
  },
  ride:{
    costSpeed:0.18,
    name:"骑行",
    gif : "https://www.iteasywords.cn/static/ride_m2-min.gif",
    speedUp:true,
  },
  swim:{
    costSpeed:0.14,
    name:"游泳",
    gif : "https://www.iteasywords.cn/static/swim_f0.gif",
    speedUp:true,
  },
  run:{
    costSpeed:0.17,   // 1s消耗多少cal
    name:"跑步",
    gif : "https://www.iteasywords.cn/static/run.gif",
    speedUp:true,
  },
  tiaosheng:{
    costSpeed:0.22,
    name:"跳绳",
    gif : "https://www.iteasywords.cn/static/tiaosheng0.gif",
  },
  basketball:{
    costSpeed:0.28,
    name:"篮球",
    gif : "https://www.iteasywords.cn/static/basketball0-500.gif",
  },
  yangwoqizuo:{
    costSpeed:0.3,
    name:"仰卧",
    gif : "https://www.iteasywords.cn/static/yangwoqizuo_m0.gif",
  },
  fuwocheng:{
    costSpeed:0.4,
    name:"俯卧撑",
    gif : "https://www.iteasywords.cn/static/fuwocheng0.gif",
  },
  dumbbell:{
    costSpeed:0.5,
    name:"哑铃",
    gif : "https://www.iteasywords.cn/static/yaling-500.gif",
  },
  stand:{
    costSpeed:0,
    name:"站立",
    gif : "https://www.iteasywords.cn/static/dadun.gif",
  }, 
}

var stages = {
  still:{
    id:"still",
    ratio:1,          // 消耗系数
    next:"slow",
    time:1,           // 当前阶段点击多少秒进入下个阶段
  },
  slow:{
    id:"slow",
    musicRate:0.8,
    ratio:1,
    next:"normal",
    time:3,
  },
  normal:{
    id:"normal",
    musicRate:1,
    ratio:1.2,
    next:"fast",
    time:5,
  },
  fast:{
    id:"fast",
    ratio:1.5,
    next:"fly",
    musicRate:1.5,
    time:8,
  },
  fly:{
    id:"fly",
    musicRate:2,
    ratio:2,
    next:"",
    time:0,
  },
}

//获取当前日期函数
function getNowFormatDate() {
  let date = new Date(),
  year = date.getFullYear(), //获取完整的年份(4位)
  month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
  strDate = date.getDate() // 获取当前日(1-31)
  if (month < 10) month = "0" + month // 如果月份是个位数，在前面补0
  if (strDate < 10) strDate = "0" + strDate // 如果日是个位数，在前面补0
 
  return year + month + strDate
}

function dealTodayCost(dbData) {
  var todayStr = getNowFormatDate()
  if (dbData.today != todayStr){
    dbData.today = todayStr
    dbData.todaycost = 0
  }
}

function formatNumber(num){
  return String(Math.floor(num * 100) / 100)
}

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}


module.exports = {
	text,makeLanguageText,observeText,HttpPost,URL,timestampToTime,timestampToDate,
	getNowFormatDate,stages,exerciseTypes,dealTodayCost,formatNumber,getRandomInt,
}


